#include "bullet.h"

namespace sprites
{
	Bullet::Bullet(Vector3D _pos, MatterType _type, float _boundRadius)
		: Sprite(_pos, _type, _boundRadius, 1.0f)
	{
		velocity = Vector3D(0, 0.5f, 0);
		build();
	}
	Bullet::Bullet(Vector3D _pos, MatterType _type, float _boundRadius, float scale)
		: Sprite(_pos, _type, _boundRadius, scale)
	{
		velocity = Vector3D(0, 0.5f, 0);
		build();
	}
	Bullet::Bullet(Vector3D _pos, MatterType _type, float _boundRadius, Vector3D _velocity, float scale)
		: Sprite(_pos, _type, _boundRadius, scale)
	{
		velocity = _velocity;
		build();
	}

	Bullet::~Bullet()
	{
		root->destroy();
	}

	//building functions generate the object's representation on game screen.
	void Bullet::build()
	{
		bindingShapes.push_back(bound);
		buildTop();
		buildMiddle();
		buildBottom();
	}

	void Bullet::buildTop()
	{
		Cylinder* cylinder = new Cylinder(0.25f, 0.5f, 0xFF010101);
		cylinder->setMajorAxis(Vector3D(0.0f, 1.0f, 0.0f));
		cylinder->textured = true;
		cylinder->setTexture("resources/textures/kitty.jpg");
		cylinder->setTranslation(Vector3D(-0.15f, 0.0f, 0.0f));
		root->addChild(cylinder);
	}

	void Bullet::buildMiddle()
	{
		Cylinder* cylinder = new Cylinder(0.25f, 0.5f, 0xFF010101);
		cylinder->setMajorAxis(Vector3D(0.0f, 1.0f, 0.0f));
		cylinder->setTranslation(Vector3D(0.0f, -0.45f, 0.0f));
		root->addChild(cylinder);
	}

	void Bullet::buildBottom()
	{
		Cylinder* cylinder = new Cylinder(0.25f, 0.5f, 0xFF010101);
		cylinder->setMajorAxis(Vector3D(0.0f, 1.0f, 0.0f));
		cylinder->textured = true;
		cylinder->setTexture("resources/textures/kitty.jpg");
		cylinder->setTranslation(Vector3D(-0.15f, -0.9f, 0.0f));
		root->addChild(cylinder);
	}

	//for collision handling
	BindingSphere Bullet::getBindingSphere()
	{
		bound.moveTo(parent.getTranslation());
		return bound;
	}

	std::vector<BindingSphere> Bullet::getBindingShapes()
	{
		bound.moveTo(parent.getTranslation());
		bindingShapes = vector<BindingSphere>();
		bindingShapes.push_back(bound);
		return bindingShapes;
	}
	
	void Bullet::handleCollision(MatterType oType, BindingSphere sphere)
	{
		switch(oType)
		{
		case collisions::BARRIER:
			dead = true;
			break;
		case collisions::SHIPBULLET :
			//blow up some of the barrier
			break;
		case collisions::INVADERBULLET :
			//blow up some of the barrier
			break;
		case collisions::INVADER :
			if(type == collisions::SHIPBULLET)
				dead = true;
			//do nothing
			break;
		case collisions::SHIP :
			if(type == collisions::INVADERBULLET)
				dead = true;
			//huh? O.o
			break;
		}
	}

	//for drawing
	void Bullet::draw()
	{
		if(parent.getTranslation().Y > 25 || parent.getTranslation().Y < -20)
		{
			dead = true;
		}
		*this+=velocity;
		parent.draw();
	}
}